
/**
 * @author BlobbyCompiloTeam
 *
 */
public class MaPASM extends MaP{
	
	public MaPASM(String str){
		super(str);
	}
	
	
	public void entete(){
		String str = ".MODEL small\n" +
				".586\n" +
				"EXTRN lirent:proc, ecrent:proc\n" +
				"EXTRN ecrbool:proc\n" +
				"EXTRN ecrch:proc, ligsuiv:proc\n" +
				".CODE\n";
		Ecriture.ecrireStringln(out,str);
	}
	
	public void ouvprinc(int taille){
		String str = "sub sp," + taille + "\n" +
				"mov bp,sp";
		Ecriture.ecrireStringln(out,str);
	}
	
	public void queue(){}
	

	public void etiq_princ() {
		String str = "debut:\n" + "STARTUPCODE";
		Ecriture.ecrireStringln(out,str);
	}
	
	
	public void etiq_fprinc() {
	}
	

	public void retourSyst(){
		String str = "EXITCODE" + "\n" + "end debut";
		Ecriture.ecrireStringln(out,str);
	}	
	

	public void a_la_ligne(){
		String str = "call ligsuiv";
		Ecriture.ecrireStringln(out,str);
	}
	

	public void lire_ent(int offset){
		String str = "lea dx,[bp+" + offset + "]\n" +
				"push word ptr dx\n" +
				"call lirent";
		Ecriture.ecrireStringln(out,str);
	}
	

	public void ecrire_ent(){
		String str = "call ecrent";
		Ecriture.ecrireStringln(out,str);
	}
	

	public void ecrire_bool(){
		String str = "call ecrbool";
		Ecriture.ecrireStringln(out,str);
	}
	

	public void ecrire_chaine(String chaine, int nbChaine){
		String str = ".DATA\n" +
				"mess" + nbChaine + " db \"" + chaine + "$\"\n" +
				".CODE\n" +
				"lea dx, mess" + nbChaine + "\n" +
				"push dx\n" +
				"call ecrch";
		Ecriture.ecrireStringln(out,str);
	}
	

	public void aff_var(int offset){
		String str = "pop ax\n" +
				"mov [bp+" + offset + "],ax";
		Ecriture.ecrireStringln(out,str);
	}
	

	public void emp_val(int val) {
		Ecriture.ecrireStringln(out, "push word ptr " + val);
	}
	

	public void emp_var(int val) {
		Ecriture.ecrireStringln(out, "push word ptr [bp+" + val + "]");
	}

	public void add() {
		String str="pop ax \n" +
				"pop bx \n" +
				"add ax,bx \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void sous() {
		String str="pop bx \n" +
				"pop ax \n" +
				"sub ax,bx \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);	
	}
	

	public void mul() {
		String str="pop bx \n" +
				"pop ax \n" +
				"imul bx \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void div() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cwd \n" +
				"idiv bx \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void ou() {
		String str="pop ax \n" +
				"pop bx \n" +
				"or ax,bx \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);
	}

	public void et() {
		String str="pop ax \n" +
				"pop bx \n" +
				"and ax,bx \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void inf() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cmp ax,bx \n" +
				"jl $+6\n" +
				"push word ptr 0\n" +
				"jmp $+4\n" +
				"push word ptr -1";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void infeq() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cmp ax,bx \n" +
				"jle $+6\n" +
				"push word ptr 0\n" +
				"jmp $+4\n" +
				"push word ptr -1";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void sup() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cmp ax,bx \n" +
				"jg $+6\n" +
				"push word ptr 0\n" +
				"jmp $+4\n" +
				"push word ptr -1";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void supeq() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cmp ax,bx \n" +
				"jge $+6\n" +
				"push word ptr 0\n" +
				"jmp $+4\n" +
				"push word ptr -1";
		Ecriture.ecrireStringln(out, str);
	}
	

	public void eq() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cmp ax,bx \n" +
				"je $+6\n" +
				"push word ptr 0\n" +
				"jmp $+4\n" +
				"push word ptr -1";
		Ecriture.ecrireStringln(out, str);	
	}
	

	public void neq() {
		String str="pop bx \n" +
				"pop ax \n" +
				"cmp ax,bx \n" +
				"jne $+6\n" +
				"push word ptr 0\n" +
				"jmp $+4\n" +
				"push word ptr -1";
		Ecriture.ecrireStringln(out, str);	
	}

	public void non() {
		String str="pop ax \n" +
				"not ax \n" +
				"push ax";
		Ecriture.ecrireStringln(out, str);
	}
	
	public void moins() {
		String str="pop ax \n" +
					"neg ax \n" +
					"push ax";
		Ecriture.ecrireStringln(out, str);
	}
	
	public void fermer() {
		
	}
	
	public void iter_ttq(int numIter) {
		
		Ecriture.ecrireStringln(out, "ttq" + numIter + ":");
	}
	
	public void iter_faire(int numIter) {
		
		Ecriture.ecrireStringln(out, "pop ax\n" +"cmp ax,-1\n" +
				"jne fait" + numIter);
	}

	public void iter_fait(int numIter) {
		
		Ecriture.ecrireStringln(out, "jmp ttq"+numIter+"\n" +
				"fait" + numIter + ":");
	}
	
	public void cond_sinon(int numCond){
		Ecriture.ecrireStringln(out, 	"jmp fsi" + numCond + "\naut" + numCond + ":");
	}
	
	public void cond_si(int numCond)
	{
		Ecriture.ecrireStringln(out, 	"pop ax\n" +"cmp ax, -1\n" + "jne aut" + numCond);
												
	}
	
	public void cond_fsi(int numCond)
	{
		Ecriture.ecrireStringln(out, 	"fsi" + numCond + ":");
		
	}
	
	public void fonc_nom(String nom) {
		super.fonc_nom(nom);
	}
	
	public void fonc_ouv_bloc(int num) {
		Ecriture.ecrireStringln(out, "enter " + num + ", 0");
	}

	public void fonc_emp_vparf(int num) {
		Ecriture.ecrireStringln(out, "mov ax,word ptr[bp+" + num + "] \n" +
				"push ax");
	}
	
	public void fonc_retourneVal(int val, String nom) {
		Ecriture.ecrireStringln(out, "pop ax \n" +
				"mov word ptr[bp+" + val + "],ax \n" + "jmp f" + nom);
	}
	
	public void fonc_ferme_bloc(int num) {
		Ecriture.ecrireStringln(out, "leave \n" +
									 "ret " + num +"\n");
	}
	
	public void fin_bloc(String nom){
		Ecriture.ecrireStringln(out, "f" + nom+":");
	}
	
	public void fonc_val_retour() {
		Ecriture.ecrireStringln(out, "sub sp,2");
	}
	
	public void fonc_appel_proc(String nom) {
		Ecriture.ecrireStringln(out, "call " + nom);
	}
	
}